package interview;

import java.util.Arrays;

public class Solution02 {
    public static void main(String[] args) {
        //seats = [1,0,0,0,1,0,1]
        int[] seats = new int[]{0,1};
        System.out.println(max(seats));
    }
    public static int max(int[] seats) {
        int n = seats.length;
        int[] maxLeft = new int[n];
        int[] maxRight = new int[n];
        Arrays.fill(maxLeft, -1);
        int i = 0;
        for (; i < n; i++) {
            if (seats[i] == 1) break;
        }
        maxLeft[i] = 0;
        for (i += 1; i < n; i++) {
            if (seats[i] != 1) {
                maxLeft[i] = maxLeft[i - 1] + 1;
            }
        }
        Arrays.fill(maxRight, -1);
        for (i = n - 1; i >= 0; i--) {
            if (seats[i] == 1) break;
        }
        maxRight[i] = 0;
        for (i -= 1; i >= 0; i--) {
            if (seats[i] != 1) {
                maxRight[i] = maxRight[i + 1] + 1;
            }
        }
        int res = 0;
        for (int j = 0; j < n; j++) {
            int tmp = 0;
            if (maxLeft[j] > 0 && maxRight[j] > 0) tmp = Math.min(maxLeft[j], maxRight[j]);
            else if (maxLeft[j] < 0) tmp = maxRight[j];
            else if (maxRight[j] < 0) tmp = maxLeft[j];
            res = Math.max(res, tmp);
        }
        return res;
    }
}
